﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace OrderAggregator.InputFormats.Lenta
{
    /// <summary>
    /// Паресер заявок с Ленты
    /// </summary>
    public class LentaFormat : BaseOrderFormat
    {
        public readonly static LentaFormat Instance = new LentaFormat();

        private LentaFormat()
        {
        }

        /// <summary>
        /// Parse
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="getStream"></param>
        /// <returns></returns>
        public override BaseOrder Parse(string filePath, Func<System.IO.Stream> getStream)
        {

            if (!string.Equals(System.IO.Path.GetExtension(filePath), dotXML, StringComparison.InvariantCultureIgnoreCase))
            {
                return null;
            }






            XDocument doc;
            using (var reader = new XmlTextReader(getStream()))
            {
                doc = XDocument.Load(reader);
            }

            var order = new BaseOrder();

            //header
            {
                var headerTag = doc.Root.Element("Order-Header");
                var partiesTag = doc.Root.Element("Order-Parties");


                order.Id = ((string)headerTag.Element("OrderNumber")).Trim();
                order.Client = ((string) partiesTag.Element("DeliveryPoint")).Trim();

                string strVal = (string) headerTag.Element("ExpectedDeliveryDate");
                order.ReceiveTime = DateTime.Parse(strVal, RuCulture);

                strVal = (string) headerTag.Element("ExpectedDeliveryTime");
                order.ReceiveTime = order.ReceiveTime.Date.Add(TimeSpan.Parse(strVal));
            }

            //rows
            {
                var linesTag = doc.Root.Element("Order-Lines");
                var lineItemTags = linesTag.Descendants("Line-Item");

                
                foreach (var lineItemTag in lineItemTags)
                {
                    var orderRow = new BaseOrderRow();

                    orderRow.ProductCode = (string)lineItemTag.Element("BuyerItemCode");
                    orderRow.ProductName = (string)lineItemTag.Element("ItemDescription");
                    orderRow.UnitName = ParseUnitName((string)lineItemTag.Element("UnitOfMeasure"));

                    orderRow.Count = decimal.Parse((string) lineItemTag.Element("OrderedQuantity"), System.Globalization.CultureInfo.InvariantCulture);
                    order.OrderRows.Add(orderRow);
                }
            }

            return order;
        }

        private static string ParseUnitName(string internalUnitName)
        {
            switch(internalUnitName)
            {
                case "KGM":
                    return "кг";

                case "PCE":
                    return "шт";
            }

            return internalUnitName;
        }
    }
}
